<?php

function blog_url_item($id,$alias){
	return HOME."blog/$id/$alias.html";
}
function blog_url_user($username){
	return HOME."blog/user/$username";
}
function blog_url_category($id,$alias){
	return HOME."blog/c$id/$alias";
}
function blog_url_subcategory($id,$alias){
	return HOME."blog/s$id/$alias";
}
function blog_url_tag($alias){
	return HOME."blog/tag/$alias";
}
//End url video function
function blog_get_category_of_item($id,$sub=''){
		$db=new db;
		$db->connect();
		// Main category
		$db->query("select * from blog where id=".$id);
		if($db->num_rows()>0){
				$dt['main']=$db->fetchOne();
				$dt['main']['url']=blog_url_category($dt['main']['id'],$dt['main']['alias']);
		}
		//Subcategory
		if($sub!=''){
				$db->query("select * from blog where id=".$sub);
				if($db->num_rows()>0){
					$dt['sub']=$db->fetchOne();
					$dt['sub']['url']=blog_url_subcategory($dt['sub']['id'],$dt['sub']['alias']);
				}
		}else{
			$db->query("select * from blog where parent=".$id." order by thutu asc");
				if($db->num_rows()>0){
					$vals=$db->fetchAll();
					foreach($vals as $val){
						$val['url']=blog_url_subcategory($val['id'],$val['alias']);
						$dt['sub'][]=$val;
					}
					
				}
		}
		$db->close();
		return $dt;
}

function blog_get_subcategory_of_category($id){
		$db=new db;
		$db->connect();
		// Main category
		$db->query("select * from blog where parent=".$id);
		if($db->num_rows()>0){
				$row=$db->fetchOne();
				$row['url']=blog_url_subcategory($row['id'],$row['alias']);
		}else{
			return false;
		}
		//Subcategory
	
		$db->close();
		return $row;
}

function blog_get_user_post($username){
	
}
function blog_add_tag_xcrud($id,$str){
		
			if($str){
					
						$db=new db;
						$db->connect();
							$tagar=explode(",",$str);
										foreach($tagar as $value ){
											$value=trim($value);
											$tag=khongdau($value);
											if($tag){
											$db->query("select * from blog_tag where type='blog' and alias ='".$tag."'");
													if($db->num_rows()>0){
															$row=$db->fetchOne();
														 	$db->nonquery("update blog_tag set num=num+1 where id=".$row['id']);
														}else{
															$db->nonquery("insert into blog_tag(name,alias,type,username) values('".$value."','".$tag."','blog','".$_SESSION['user']['username']."')");
														}
											}// Neu tag ko rong
										}
										
						$db->close();
							
				}

}

function blog_get_all_category(){
	$db=new db;
	$db->connect();
	$db->query("select id,name,alias from blog where type='blog' and parent is NULL order by thutu asc");
	$val=$db->fetchAll();
	foreach($val as $row){
		$row['url']=blog_url_category($row['id'],$row['alias']);
		$db->query("select id,name,alias from blog where type='blog' and parent=".$row['id']." order by thutu asc");
		if($db->num_rows()>0){
			$val2=$db->fetchAll();
			foreach($val2 as $row2){
				$row2['url']=blog_url_subcategory($row2['id'],$row2['alias']);
				$row['sub'][]=$row2;
			}
			
		}
		$data[]=$row;
	}
	
	return $data;
}

function blog_get_comment_last_page_data($id,$page,$rows_per_page,$blog_create_user){
	//$id blog_id, $page : Max page
					$start=$rows_per_page*($page-1);
					$sql ="SELECT * from blog_comment where blog_id=$id  order by id asc limit $start,$rows_per_page";
					$db=new db;
					$db->connect();
					$db->query($sql);
					if($db->num_rows()>0){
					$data=$db->fetchAll();
						foreach($data as $row){
								$db->query("select name,avatar,username,date_registered from users where username='".$row['upload_user']."'");
								$row['user']=$db->fetchOne();
								$vals[]=$row;
						}
					}
					
					//Return render content html
					return blog_render_comment_ajax($vals,$blog_create_user);
	
}

function blog_get_comment_last_page($id,$rows_per_page){
		$sql="SELECT id from blog_comment where  blog_id=$id ";
		$db=new db;
		$db->connect();
		$db->query($sql);
		$total_rows=$db->num_rows();
		if ($total_rows == 0) {
			return false;
		}
		$max_pages = ceil($total_rows /$rows_per_page );
		return $max_pages;
	}
	
function blog_get_posts($alias,$alias_sub,$id='',$num){
		$db=new db;
		$db->connect();
		
	switch($alias){
		case "home":
		
			if($alias_sub=='pick'){
				$db->query("select id,name,alias,upload_user,upload_time from blog_post where pick_home=1 and status=1 order by id desc limit 0,$num");
				if($db->num_rows()>0){
					$post_new=$db->fetchAll();
					foreach($post_new as $r4){
						$r4['url']=blog_url_item($r4['id'],$r4['alias']);
						$db->query("select name,avatar from users where username='".$r4['upload_user']."'");
						$r4['user']=$db->fetchOne();
						$data[]=$r4;
					}
				}
				
			}//End pick
			if($alias_sub=='new'){
				$db->query("select id,name,alias,upload_user,upload_time from blog_post where  status=1 order by id desc limit 0,$num");
				if($db->num_rows()>0){
					$post_new=$db->fetchAll();
					foreach($post_new as $r4){
						$r4['url']=blog_url_item($r4['id'],$r4['alias']);
						$db->query("select name,avatar from users where username='".$r4['upload_user']."'");
						$r4['user']=$db->fetchOne();
						$data[]=$r4;
					}
				}
				
			}//End new
			if($alias_sub=='comment'){
				$db->query("select id,name,alias,upload_user,upload_time from blog_post where  status=1 order by update_time desc limit 0,$num");
				if($db->num_rows()>0){
					$post_new=$db->fetchAll();
					foreach($post_new as $r4){
						$r4['url']=blog_url_item($r4['id'],$r4['alias']);
						$db->query("select name,avatar from users where username='".$r4['upload_user']."'");
						$r4['user']=$db->fetchOne();
						$data[]=$r4;
					}
				}
				
			}//End comment
			
			
			break;
		//End case "home";
		case "category":
			if($alias_sub=='pick'){
				$db->query("select id,name,alias,upload_user,upload_time from blog_post where pick_category=1 and status=1 and category=$id order by id desc limit 0,$num");
				if($db->num_rows()>0){
					$post_new=$db->fetchAll();
					foreach($post_new as $r4){
						$r4['url']=blog_url_item($r4['id'],$r4['alias']);
						$db->query("select name,avatar from users where username='".$r4['upload_user']."'");
						$r4['user']=$db->fetchOne();
						$data[]=$r4;
					}
				}
				
			}//End pick
			
			
			if($alias_sub=='new'){
				$db->query("select id,name,alias,upload_user,upload_time from blog_post where  status=1 and category=$id order by id desc limit 0,$num");
				if($db->num_rows()>0){
					$post_new=$db->fetchAll();
					foreach($post_new as $r4){
						$r4['url']=blog_url_item($r4['id'],$r4['alias']);
						$db->query("select name,avatar from users where username='".$r4['upload_user']."'");
						$r4['user']=$db->fetchOne();
						$data[]=$r4;
					}
				}
				
			}//End new
			
			if($alias_sub=='comment'){
				$db->query("select id,name,alias,upload_user,upload_time from blog_post where   category=$id and  status=1  order by update_time desc limit 0,$num");
				if($db->num_rows()>0){
					$post_new=$db->fetchAll();
					foreach($post_new as $r4){
						$r4['url']=blog_url_item($r4['id'],$r4['alias']);
						$db->query("select name,avatar from users where username='".$r4['upload_user']."'");
						$r4['user']=$db->fetchOne();
						$data[]=$r4;
					}
				}
				
			}//End comment
			break; // End category alias
			
			case "subcategory":
			if($alias_sub=='pick'){
				$db->query("select id,name,alias,upload_user,upload_time from blog_post where pick_subcategory=1 and status=1 and subcategory=$id order by id desc limit 0,$num");
				if($db->num_rows()>0){
					$post_new=$db->fetchAll();
					foreach($post_new as $r4){
						$r4['url']=blog_url_item($r4['id'],$r4['alias']);
						$db->query("select name,avatar from users where username='".$r4['upload_user']."'");
						$r4['user']=$db->fetchOne();
						$data[]=$r4;
					}
				}
				
			}//End pick
			
				
			if($alias_sub=='new'){
				$db->query("select id,name,alias,upload_user,upload_time from blog_post where  status=1 and subcategory=$id order by id desc limit 0,$num");
				if($db->num_rows()>0){
					$post_new=$db->fetchAll();
					foreach($post_new as $r4){
						$r4['url']=blog_url_item($r4['id'],$r4['alias']);
						$db->query("select name,avatar from users where username='".$r4['upload_user']."'");
						$r4['user']=$db->fetchOne();
						$data[]=$r4;
					}
				}
				
			}//End new
			
			if($alias_sub=='comment'){
				$db->query("select id,name,alias,upload_user,upload_time from blog_post where   subcategory=$id and  status=1  order by update_time desc limit 0,$num");
				if($db->num_rows()>0){
					$post_new=$db->fetchAll();
					foreach($post_new as $r4){
						$r4['url']=blog_url_item($r4['id'],$r4['alias']);
						$db->query("select name,avatar from users where username='".$r4['upload_user']."'");
						$r4['user']=$db->fetchOne();
						$data[]=$r4;
					}
				}
				
			}//End comment
			break; // End category alias
			
			
			
	} //End switch
	
	$db->close();
	return $data;
}

function blog_get_comment_right(){

		// -- Get 10 comment moi nhat
		$db=new db;
		$db->connect();
		$db->query("select * from comment  where type='blog' order by id desc limit 0,12");
		if($db->num_rows()>0){
			$data=$db->fetchAll();
            $str='';
            $j=1;
               foreach($data as $row){
					 				if($j%2==0){
									$str.='<div class="item-even each-comment-item">';
									}else{
									$str.='<div class="item-odd each-comment-item">';
									}
										$str.='<a title="'.$row["tieude"].'" href="'.$row["url"].'" class="each-comment-title">'.cutstring($row["tieude"], 28).'</a>
										<span class="comment-time">'.time_ago($row['time_add']).'</span>
										<div class="clr"></div>
										<p class="each-comment-des">'.cutstring($row['noidung'], 100).'</p>
									</div>';
					  $j++;
                   } // End foreach

		}else{
			$str= "<h4 class='text-center'>Không Có Dữ Liệu</h4>"	;
		}
	return $str;
} 